///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ZZ_DETAIL_SEQUENCE_H_
#define _BUOLA_ZZ_DETAIL_SEQUENCE_H_

#include <buola/zz.h>

#include <buola/traits/tuple.h>

namespace buola { namespace zz { namespace detail {

template<typename tIt,typename... tAll>
struct MSequenceAttr
{
    typedef typename MCreateTuple<typename tAll::template tAttribute<tIt>::tType...>::type tOriginalTuple;
    typedef MCollapseTuple<tOriginalTuple> tCollapse;
    typedef typename tCollapse::type tAttribute;
    typedef typename tCollapse::tCollapseType tCollapseType;
};

template<typename tCollapseType,bool pUnused,int i>
struct MSequenceGetter;

//it was collapsed into an empty tuple, which means that all elements have unused attribute
template<int i>
struct MSequenceGetter<MCollapseTypeEmpty,true,i>
{
    static UUnused &Get(UUnused &pAttr,UUnused&)
    {
        return pAttr;
    }
};

//it was collapsed into a single element.
//    case where this attribute is unused... return the dummy
template<int i>
struct MSequenceGetter<MCollapseTypeSingle,true,i>
{
    template<typename tType>
    static UUnused &Get(tType& /*pAttr*/,UUnused &pDummy)
    {
        return pDummy;
    }
};

//    case where this attribute is the single non-dummy element... return it
template<int i>
struct MSequenceGetter<MCollapseTypeSingle,false,i>
{
    template<typename tType>
    static tType &Get(tType &pAttr,UUnused&)
    {
        return pAttr;
    }
};

//it was collapsed into a vector.
//    case where this attribute is unused... return the dummy
template<int i>
struct MSequenceGetter<MCollapseTypeVector,true,i>
{
    template<typename tType>
    static UUnused &Get(tType& /*pAttr*/,UUnused &pDummy)
    {
        return pDummy;
    }
};

//    case where this element is in the vector... return the current position within it
// even if it was collapsed into a vector, the user might provide a tuple or pair
template<int i>
struct MSequenceGetter<MCollapseTypeVector,false,i>
{
    //we can still get a UUnused as attribute
    static UUnused &Get(UUnused &pAttr,UUnused&)
    {
        return pAttr;
    }

    template<typename tType>
    static tType &Get(std::vector<tType> &pAttr,UUnused&)
    {
        if(pAttr.size()<i+1)
            pAttr.resize(i+1);
        return pAttr[i];
    }

    template<typename... tElems>
    static typename std::tuple_element<i,std::tuple<tElems...>>::type& Get(std::tuple<tElems...> &pTuple,UUnused& /*pDummy*/)
    {
        return std::get<i>(pTuple);
    }

    template<typename tFirst,typename tSecond>
    static typename std::conditional<i==0,tFirst&,tSecond&>::type Get(std::pair<tFirst,tSecond> &pPair,UUnused& /*pDummy*/)
    {
        return std::get<i>(pPair);
    }
};

//it was not collapsed
//    case where this attribute is unused... return the dummy
template<int i>
struct MSequenceGetter<MCollapseTypeNoCollapse,true,i>
{
    template<typename tType>
    static UUnused &Get(tType& /*pAttr*/,UUnused &pDummy)
    {
        return pDummy;
    }
};

//    case where this element is in the vector... return the current position within it
template<int i>
struct MSequenceGetter<MCollapseTypeNoCollapse,false,i>
{
    //we can still get a UUnused as attribute
    static UUnused &Get(UUnused &pAttr,UUnused&)
    {
        return pAttr;
    }

    template<typename... tElems>
    static typename std::tuple_element<i,std::tuple<tElems...>>::type& Get(std::tuple<tElems...> &pTuple,UUnused& /*pDummy*/)
    {
        return std::get<i>(pTuple);
    }

    template<typename tFirst,typename tSecond>
    static typename std::conditional<i==0,tFirst&,tSecond&>::type Get(std::pair<tFirst,tSecond> &pPair,UUnused& /*pDummy*/)
    {
        static_assert(i<2,"can't use pair with more than two elements");
        return std::get<i>(pPair);
    }
};

/*namespace detail*/ } /*namespace zz*/ } /*namespace buola*/ }

#endif
